/*!
 * 下拉窗口控件.
 * ecp.qzz.ui.drop.popwindow.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.window","qzz.button"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.drop");
	/**
	 * 下拉窗口控件.
	 */
	qzz.ui.drop.popwindow = qzz.Class(qzz.ui.window, {
		/**
		 * 创建.
		 */
	    _create:function() {
		    this.base();
		    this.className = "PopWindow";
            //this.logInfo("popWindow.create");
            //显示状态
            this._hasShow = false;
            //是否可以隐藏
            this._canHide = true;
            this.getOption().closeImg = true;this.getBooleanValue(this.getOption().closeImg, false);
            //宽高
            this.getOption().width = this.getNumberValue(this.getOption().width, 200);
            this.getOption().height = this.getNumberValue(this.getOption().height, 200);
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {
	    	this.base();
	    	this.setStyle(this.thisWindow, "dropDiv");
	    	this.thisWindow.style.display = "none";
	    	if(this.getOption().closeImg) {
	    	    this.closeDiv = new qzz.ui.button(
	        	{"top":0,"left":this.getOption().width-20,"width":20,"height":20,"fontSize":12,
	            "common":["button_common"],
		        "over":["button_over"],
		        "down":["button_over"]});
	    	}	    	
	    	this.thisWindow.oncontextmenu = function(){
	    		return false;
	    	};
	    },
	    /**
	     * 执行焦点事件.
	     * @param ev 事件对象
	     */
	    doFocus:function(ev) {
	    	if(this.isShow()) {
		    	this.base();
	    	}
	    },
	    /**
	     * 执行失去焦点事件
	     * @param ev 事件对象
	     */
        doBlur:function(ev) {
	    	if(this.getOption().closeImg && this.isShow() && (this.isMouseOver == null || this.isMouseOver() !== true)) {
	    		this.hide();
	    		this.base();
	    	}
	    },
	    /**
	     * 获取下拉窗口.
	     * @return 返回元素
	     */
	    getDropWindow:function() {
	    	return this.thisWindow;
	    },
	    /**
	     * 是否已显示.
	     * @return 状态
	     */
	    isShow:function(){
	    	return this.thisWindow != null && this.thisWindow.style.display != "none";
	    },
	    /**
	     * 下拉选择.
	     * @param top 顶点
	     * @param left 左边
	     */
	    execute:function(top, left, pHeight, pWidth){
	    	this.logInfo("show Drop Window.");
	    	var space = 5;
	    	var sxy = this.getScrollXY();
	    	if(top + this.getHeight() > this._getBodyHeight() - space) {
	    		if(pHeight == null || isNaN(pHeight)) {
	    			pHeight = 0;
	    		}
	    		top = top - this.getHeight() - pHeight - space;
	    		if(top < 0) {
	    			top = this._getBodyHeight() - this.getHeight() - space;
	    		}
	    	}
	    	if(left + this.getWidth() > this._getBodyWidth() - space + sxy.left) {
	    		if(pWidth == null || isNaN(pWidth)) {
	    			pWidth = 0;
	    		}
	    		left = left - this.getWidth() - pWidth - space;
	    		if(left < 0) {
	    			left = this._getBodyWidth() - this.getWidth() - space;
	    		}
	    	}
	    	var pele = this.getControl()[0].parentNode;
	    	if(pele.tagName !== "BODY") {
	    		if(pele.scrollTop > 0) {
	    			top += pele.scrollTop;
	    		}
	    		if(pele.scrollLeft > 0) {
	    			left += pele.scrollLeft;
	    		}
	    	}
	    	//要随着父级窗口的调整进行调整。
	    	var pDom = this.thisWindow.parentElement;
	    	if(pDom != null) {
	    		var ptop = this.getNumberValue(pDom.style.top, 0);
	    		var pleft = this.getNumberValue(pDom.style.left, 0);
	    		top -= ptop;
	    		left -= pleft;
	    	}
	    	this.getOption().top = (top + 4);
	    	this.thisWindow.style.top = this.getOption().top + "px";
	    	this.getOption().left = left;
	    	this.thisWindow.style.left = left + "px";
	        if (!this.isShow()) {
	            this.thisWindow.style.display = "";
	            this.hasSelect = true;
	            if(typeof this.onShow == "function") {
	            	try {
	            		this.onShow(this);
	            	} catch(e) {
	            		this.logInfo("[onShow]ERR:" + e.message);
	            	}
	            }
	            this.trigger("onShow", [this]);
	        }
	        this.setFocus();
	    },
	    /**
	     * 执行键盘按下事件.
	     */
	    doKeyDown:function(ev, key) {
	    	if(key === this.keyBoard.ESC) {
	    		this.hide();
	    	}
	    },	
	    /**
	     * 隐藏窗口.
	     */
	    hide:function(){
	    	if (this.isShow() && this._canHide) {
                this.thisWindow.style.display = "none";
                this.setBlur();
                if(typeof this.onHide == "function") {
	            	try {
	            		this.onHide(this);
	            	} catch(e) {
	            		this.logInfo("[onHide]ERR:" + e.message);
	            	}
	            }
                this.trigger("onHide", [this]);
	    	}
	    }
	});
	_bindRenderFunc("qzzpopwin", qzz.ui.drop.popwindow);
	return qzz.ui.drop.popwindow;
});